Composite Figures

Elizabeth King
Kevin Middleton

Composite figures

  • Combining separate plots (e.g., not with facet_grid())
    • Complex arrangements
  • Adding labels
  • Adjusting margins and axes to line up

Packages

Two highly functional packages:

  1. cowplot (classic)
  2. patchwork (newer)
  • Largely complimentary sets of functions.
  • Some customization are easier with one vs. the other.

Generate figures

set.seed(4534875)

labeler <- function(x) {
  annotation_custom(textGrob(x, gp = gpar(fontsize = 30)), 
                    xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
}

M <- tibble(x = rpois(50, 5),
            n = 1:50)

p1 <- ggplot(M, aes(x)) + 
  geom_histogram(bins = 30, fill = "orange") +
  scale_x_continuous(breaks = seq(0, 10, by = 2.5), 
                     labels = seq(0, 1000, by  = 250)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  labeler("Plot 1")
p2 <- ggplot(M, aes(x)) + 
  geom_density() +
  labeler("Plot 2")
p3 <- ggplot(M, aes(x = n, y = x)) + 
  geom_path(color = "darkgreen")  +
  scale_y_continuous(breaks = seq(0, 10, by = 2.5), 
                     labels = seq(0, 1000, by  = 250)) +
  labeler("Plot 3")
p4 <- ggplot(M, aes(x = x, y = n)) +
  geom_path(color = "purple")  +
  labeler("Plot 4")

Basics of cowplot: plot_grid()

  • Plots in order
  • Fill by row
plot_grid(p1, p2, p3, p4, nrow = 2)

Basics of cowplot: plot_grid()

Empty spaces in the grid

plot_grid(p1, p2, NULL, p4, nrow = 4)

Empty spaces in the grid

Adding and formatting labels

plot_grid(p1, p2, p3, p4, nrow = 2,
          labels = c("a.", "b.", "c.", "d."),
          label_fontface = "italic",
          label_size = 12)

Adding and formatting labels

Aligning subplots

plot_grid(p1, p2, p3, p4, nrow = 2,
          labels = c("a.", "b.", "c.", "d."),
          align = "hv")

Aligning subplots

Basics of patchwork

  • patchwork uses +, |, /, and ( ) for arrangement
    • + and | work similarly in most cases
  • Axis alignment is default
(p1 + p2) / (p3 + p4)

Basics of patchwork

Automatic plot wrapping

  • If you are collecting plots in a list or from a function
  • Also works without a list
wrap_plots(list(p1, p2, p3, p4))

Automatic plot wrapping

Complex arrangements

  • 2 plots on row one, 1 plot spanning row 2
  • Can do this with plot_grid() by nesting two calls to plot_grid()
(p1 | p2) / p3

Complex arrangements

Adding labels

  • plot_annotation() adds annotation elements to the patchwork
(p1 | p2) / p3 +
  plot_annotation(tag_levels = 'a', tag_suffix = ".")

Adding labels

Adding titles

(p1 | p2) / p3 +
  plot_annotation(
    tag_levels = 'a',
    tag_suffix = ".",
    title = "Long title that spans across both plots if we add enough words.")

Adding titles

Theming

  • & theme() can globally change many plot elements
  • Make all the text serif font (try named fonts)
(p1 | p2) / p3 +
  plot_annotation(
    tag_levels = 'a',
    tag_suffix = ".",
    title = "Long title that spans across both plots if we add enough words.") &
  theme(text = element_text("serif"))

Theming